package com.huaju.enity;

import java.sql.Timestamp;
import java.util.List;
import java.util.Set;

import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import static org.hibernate.criterion.Example.create;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;


/**
 * A data access object (DAO) providing persistence and search support for
 * Account entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.huaju.enity.Account
 * @author MyEclipse Persistence Tools
 */
@Transactional
public class AccountDAO {
	private static final Logger log = LoggerFactory.getLogger(AccountDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String PHONE_MODEL = "phoneModel";
	public static final String SEX = "sex";
	public static final String IMG = "img";
	public static final String TEL = "tel";
	public static final String PWD = "pwd";
	public static final String OPEN_ID = "openId";
	public static final String ISJOB = "isjob";
	public static final String ETR = "etr";

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	protected void initDao() {
		// do nothing
	}

	public void save(Account transientInstance) {
		log.debug("saving Account instance");
		try {
			getCurrentSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	public void saveOrUpdate(Account transientInstance) {
		log.debug("saving Account instance");
		try {
			getCurrentSession().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Account persistentInstance) {
		log.debug("deleting Account instance");
		try {
			getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Account findById(java.lang.Integer id) {
		log.debug("getting Account instance with id: " + id);
		try {
			Account instance = (Account) getCurrentSession().get(
					"com.huaju.enity.Account", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Account> findByExample(Account instance) {
		log.debug("finding Account instance by example");
		try {
			List<Account> results = (List<Account>) getCurrentSession()
					.createCriteria("com.huaju.enity.Account")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Account instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Account as model where model."
					+ propertyName + "= ?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Account> findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List<Account> findByPhoneModel(Object phoneModel) {
		return findByProperty(PHONE_MODEL, phoneModel);
	}

	public List<Account> findBySex(Object sex) {
		return findByProperty(SEX, sex);
	}

	public List<Account> findByImg(Object img) {
		return findByProperty(IMG, img);
	}

	public List<Account> findByTel(Object tel) {
		return findByProperty(TEL, tel);
	}

	public List<Account> findByPwd(Object pwd) {
		return findByProperty(PWD, pwd);
	}

	public List<Account> findByOpenId(Object openId) {
		return findByProperty(OPEN_ID, openId);
	}

	public List<Account> findByIsjob(Object isjob) {
		return findByProperty(ISJOB, isjob);
	}

	public List<Account> findByEtr(Object etr) {
		return findByProperty(ETR, etr);
	}

	public List findAll() {
		log.debug("finding all Account instances");
		try {
			String queryString = "from Account";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Integer countAll() {
		log.debug("finding all Account instances");
		try {
			String queryString = "from Account";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list().size();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public Integer countAll(String pro,Object value,String searchName,String searValue) {
		log.debug("finding all Account instances");
		try {
			String queryString = "from Account model where model."+pro+"=?  and model."+searchName+" like:sname ";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			queryObject.setParameter("sname", "%"+searValue+"%");
			return queryObject.list().size();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findByPage(Integer start,Integer size,String pro,Object value,String searchName,String searValue) {
		log.debug("finding all Account instances");
		try {
			String queryString = "from Account model where model."+pro+"=? and model."+searchName+" like:sname ";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			queryObject.setParameter("sname", "%"+searValue+"%");
			queryObject.setMaxResults(size);
			queryObject.setFirstResult(start);
			
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findByPage(Integer start, Integer size) {
		log.debug("finding all Account instances");
		try {
			String queryString = "from Account";
			Query queryObject = getCurrentSession().createQuery(queryString);

			queryObject.setMaxResults(size);
			queryObject.setFirstResult(start);

			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Integer countByProAll(String propertyName, Object value) {
		log.debug("finding all Account instances");
		try {
			String queryString = "from Account as model where model."
					+ propertyName + " like :sname";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter("sname", "%" + value + "%");
			return queryObject.list().size();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findByProPage(Integer start, Integer size, String propertyName,
			Object value) {
		log.debug("finding all Account instances");
		try {
			String queryString = "select * from account as model where model."
					+ propertyName + " like :sname";
			SQLQuery queryObject = getCurrentSession().createSQLQuery(
					queryString).addEntity(Account.class);
			queryObject.setParameter("sname", "%" + value + "%");
			queryObject.setMaxResults(size);
			queryObject.setFirstResult(start);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Account merge(Account detachedInstance) {
		log.debug("merging Account instance");
		try {
			Account result = (Account) getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Account instance) {
		log.debug("attaching dirty Account instance");
		try {
			getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Account instance) {
		log.debug("attaching clean Account instance");
		try {
			getCurrentSession().buildLockRequest(LockOptions.NONE).lock(
					instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static AccountDAO getFromApplicationContext(ApplicationContext ctx) {
		return (AccountDAO) ctx.getBean("AccountDAO");
	}
	
	public Account findUserByTelephone(String telePhone) {
		log.debug("finding Account instance with property: tel" +"" 
				+ ", value: " + telePhone);
		try {
			String queryString = "from Account as model where model.tel"
					 + "= ?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, telePhone);
			List<Account> acountlist = queryObject.list();
			if(acountlist!=null&&acountlist.size()>0){
				return acountlist.get(0);
			}else{
				return null;
			}
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
}